"""
This file helps itol.py handle datasets
See the README.txt for details
@author: Albert Wang (albertyw.mit.edu)
@date: September 16, 2010
"""

import ItolFile

class Dataset:
    """
    This class handles single datasets and 
    the dataset's associated parameters
    """
    def __init__(self):
        """ Instantiate a new dataset """
        self.params = dict({})
        self.params['File'] = ''
        self.params['Label'] = ''
        self.params['Separator'] = ''
        self.params['Type'] = ''
    
    ###Setting the dataset
    
    def set_dataset_location(self, data_location):
        """
        Set the Location of the dataset to be displayed with the tree
        @param dataLocation: the path to the dataset file
        """
        data_file = ItolFile.ItolFile()
        data_file.set_file_location(data_location)
        self.params['File'] = data_file
    
    def set_dataset_contents(self, data_contents):
        """
        Set the contents of the dataset to be displayed with the tree
        @param dataContents: the contents of the dataset
        """
        data_file = ItolFile.ItolFile()
        data_file.set_file_contents(data_contents)
        self.params['File'] = data_file
    
    def get_data_set_contents(self):
        """
        Get the dataset contents
        @return the contents of the dataset
        """
        return self.params['File'].get_file_contents()
    
    ###Setting other parameters
    def add_param_dict(self, params):
        """
        Add a dictionary of parameters to the dataset
        @param params: a dictionary of parameters
        """
        self.params.update(params)
    
    def set_param_value(self, var_name, var_value):
        """
        Set a single parameter for the dataset.  
        @param varName: the name of the parameter (e.g. dataset1label)
        @param varValue: the valu of the parameter
        """
        self.params[var_name] = var_value
    
    ###Getting other parameters
    def get_params(self, dataset_num):
        """
        Get the dictionary of params and append "dataset"+dataSetNum to the beginning of each key
        @return: the dataset dictionary
        """
        upload_params = dict({})
        for key in self.params.keys():
            upload_key = 'dataset'+str(dataset_num)+key
            upload_params[upload_key] = self.params[key]
        return upload_params
    
    def ready_upload(self):
        """
        Checks whether there are enough parameters set to be uploaded
        @return: True if there are enough parameters to be uploaded; False if incorrect parameters
        """
        if self.params['File'] == '':
            return False
        if self.params['Label'] == '':
            return False
        if self.params['Separator'] == '':
            return False
        if self.params['Type'] == '':
            return False
        return True
    
    def get_params_upload(self, dataset_num):
        """
        Get the params ready for uploading onto iTOL
        @return: the dataset dictionary
        """
        upload_params = self.get_params(dataset_num)
        upload_key = 'dataset'+str(dataset_num)+'File'
        upload_params[upload_key] = upload_params[upload_key].get_file_handle()
        return upload_params
